/*
 * graph2D
 * Copyright (c) 2009 Shun Moriya <shun@mnu.sakura.ne.jp>
 *
 * The MIT License
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include "button.h"

#define BUTTON_TEXT_SIZE		(13)
#define BUTTON_MARGIN_X			(3)
#define BUTTON_MARGIN_Y			(3)
#define BUTTON_PUSHED_MARGIN_X	(BUTTON_MARGIN_X / 2)
#define BUTTON_PUSHED_MARGIN_Y	(BUTTON_MARGIN_Y / 2)
#define BUTTON_MARGIN_SIZE_X	(BUTTON_MARGIN_X * 2)
#define BUTTON_MARGIN_SIZE_Y	(BUTTON_MARGIN_Y * 2)

namespace Graph2D
{

	Button::Button()
		: textAlignmentH(TEXT_ALIGNMENT_CENTER)
		, textAlignmentV(TEXT_ALIGNMENT_MIDDLE)
		, dirty(true)
	{
		initialize();
	}

	Button::Button(const char* text)
	{
		Button();
		initialize();
		label.setString(text);
	}

	Button::Button(const std::string& text)
	{
		Button();
		initialize();
		label.setString(text);
	}

	Button::~Button()
	{
	}

	void Button::initialize()
	{
		setType(TYPE_BUTTON);
		setFrameStyle(FRAME_STYLE_NORMAL);
		setColor(0.5f, 0.5f, 0.5f, 0.9f, 0.0f);
		setSize(BUTTON_TEXT_SIZE + BUTTON_MARGIN_SIZE_X, BUTTON_TEXT_SIZE + BUTTON_MARGIN_SIZE_Y);
	}

	void Button::setSize(const Vector2& size)
	{
		super::setSize(size);

		dirty = true;
	}

	void Button::adjustSize()
	{
		label.adjustSize();
		label.setPosition(BUTTON_MARGIN_X, BUTTON_MARGIN_Y);
		super::setSize(label.getSize() + Vector2(BUTTON_MARGIN_SIZE_X, BUTTON_MARGIN_SIZE_Y));

		dirty = true;
	}

	const std::string& Button::getString() const
	{
		return label.getString();
	}

	void Button::setString(const std::string& text)
	{
		label.setString(text);

		dirty = true;
	}

	void Button::format(const char* fmt, ...)
	{
		va_list ap;
		va_start(ap, fmt);
		label.vformat(fmt, ap);
		va_end(ap);

		dirty = true;
	}

	void Button::replace(const char* key, const char* word)
	{
		label.replace(key, word);

		dirty = true;
	}

	void Button::setFontSize(const float fontSize)
	{
		label.setFontSize(fontSize);
		tag.setFontSize(fontSize);

		dirty = true;
	}

	//! 水平方向のテキストアライメントを取得
	TextAlignmentH Button::getTextAlignmentH() const
	{
		return static_cast<TextAlignmentH>(textAlignmentH);
	}

	//! 水平方向のテキストアライメントを設定
	void Button::setTextAlignmentH(const TextAlignmentH alignment)
	{
		textAlignmentH = static_cast<unsigned char>(alignment);

		dirty = true;
	}

	//! 垂直方向のテキストアライメントを取得
	TextAlignmentV Button::getTextAlignmentV() const
	{
		return static_cast<TextAlignmentV>(textAlignmentV);
	}

	//! 垂直方向のテキストアライメントを設定
	void Button::setTextAlignmentV(const TextAlignmentV alignment)
	{
		textAlignmentV = static_cast<unsigned char>(alignment);

		dirty = true;
	}

	void Button::setTextColor(const Color& color)
	{
		label.setTextColor(color);
		tag.setTextColor(color);
	}

	void Button::onSerialize(mana_stream* stream) const
	{
		super::onSerialize(stream);
		mana_stream_mark(stream);
	}

	const std::string& Button::getTagString() const
	{
		return tag.getString();
	}

	void Button::setTagString(const std::string& text)
	{
		tag.setString(text);

		dirty = true;
	}

	void Button::formatTag(const char* fmt, ...)
	{
		va_list ap;
		va_start(ap, fmt);
		tag.vformat(fmt, ap);
		va_end(ap);

		dirty = true;
	}

	void Button::onDeserialize(mana_stream* stream)
	{
		super::onDeserialize(stream);
		mana_stream_check(stream);
	}

	void Button::onUpdate(const UpdateInfomation& updateInfomation)
	{
		if(dirty)
		{
			Vector2 newPosition;

			label.adjustSize();
			tag.adjustSize();

			switch(textAlignmentH)
			{
			case TEXT_ALIGNMENT_LEFT:
				newPosition.x = getLeftFrameSize();
				break;
			case TEXT_ALIGNMENT_CENTER:
				newPosition.x = getSize().x * 0.5f - label.getSize().x * 0.5f;
				break;
			case TEXT_ALIGNMENT_RIGHT:
				newPosition.x = getSize().x - label.getSize().x - BUTTON_MARGIN_SIZE_X;
				break;
			}

			switch(textAlignmentV)
			{
			case TEXT_ALIGNMENT_TOP:
				newPosition.y = BUTTON_MARGIN_SIZE_Y;
				break;
			case TEXT_ALIGNMENT_MIDDLE:
				newPosition.y = getSize().y * 0.5f - label.getSize().y * 0.5f;
				break;
			case TEXT_ALIGNMENT_BOTTOM:
				newPosition.y = getSize().y - label.getSize().x - BUTTON_MARGIN_SIZE_Y;
				break;
			}

			label.setPosition(newPosition);

			if(!tag.getString().empty())
			{
				newPosition.x = getSize().x - tag.getSize().x - BUTTON_MARGIN_SIZE_X;
				tag.setPosition(newPosition);
			}

			dirty = false;
		}

		label.onUpdate(updateInfomation);
		tag.onUpdate(updateInfomation);

		super::onUpdate(updateInfomation);
	}

	bool Button::onDraw(const DrawRect& drawRect)
	{
		if(!super::onDraw(drawRect))
			return false;

#if 0
		const Vector2 positionOffset(getLeftFrameSize(), 0);
		const Vector2 sizeOffset(getLeftFrameSize() + getRightFrameSize(), 0);
#else
		const Vector2 positionOffset(0, 0);
		const Vector2 sizeOffset(0, 0);
#endif
		if(!label.empty())
		{
			DrawRect newDrawRect;
			newDrawRect.position = drawRect.getDrawLeftTopPosition() + (label.getPosition() + positionOffset) * drawRect.getScale();
			newDrawRect.size = label.getSize() - sizeOffset;
			newDrawRect.scale = label.getScale() * drawRect.getScale();
			newDrawRect.updateClippingInfomation(drawRect);
			newDrawRect.color *= getColor();
			label.onDraw(newDrawRect);
		}

		if(!tag.empty())
		{
			DrawRect newDrawRect;
			newDrawRect.position = drawRect.getDrawLeftTopPosition() + (tag.getPosition() + positionOffset) * drawRect.getScale();
			newDrawRect.size = tag.getSize() - sizeOffset;
			newDrawRect.scale = tag.getScale() * drawRect.getScale();
			newDrawRect.updateClippingInfomation(drawRect);
			newDrawRect.color *= getColor();
			tag.onDraw(newDrawRect);
		}

		return true;
	}

// 押した時のコンポーネントの移動量を外部から設定できるように変更

	bool Button::onTouchesBegan(const Vector2& localPosition)
	{
#if 0
		label->setPosition(label->getPosition() + Vector2(BUTTON_MARGIN_X, BUTTON_MARGIN_Y));

		tag->setPosition(tag->getPosition() + Vector2(BUTTON_MARGIN_X, BUTTON_MARGIN_Y));
#endif
		return super::onTouchesBegan(localPosition);
	}

	bool Button::onTouchesEnded(const Vector2& localPosition)
	{
#if 0
		label->setPosition(label->getPosition() - Vector2(BUTTON_MARGIN_X, BUTTON_MARGIN_Y));

		tag->setPosition(tag->getPosition() - Vector2(BUTTON_MARGIN_X, BUTTON_MARGIN_Y));
#endif
		return super::onTouchesEnded(localPosition);
	}

	bool Button::onTouchesCancelled(const Vector2& localPosition)
	{
#if 0
		label->setPosition(label->getPosition() - Vector2(BUTTON_MARGIN_X, BUTTON_MARGIN_Y));

		tag->setPosition(tag->getPosition() - Vector2(BUTTON_MARGIN_X, BUTTON_MARGIN_Y));
#endif
		return super::onTouchesCancelled(localPosition);
	}

	void Button::onSizeChanged()
	{
		dirty = true;
		/*
		 * テキストクラスはサイズ変更イベントは発生しない
		label->onSizeChanged();
		tag->onSizeChanged();
		 */
		super::onSizeChanged();
	}

	bool Button::compare(const Button& other) const
	{
		if(!super::compare(other))
			return false;

		if(label.compare(other.label) == false)
			return false;

		if(tag.compare(other.tag) == false)
			return false;

		return true;
	}
}
